// jwt.middleware.ts
import { Injectable, NestMiddleware, UnauthorizedException } from "@nestjs/common";
import { Request, Response, NextFunction } from 'express';
import * as jwt from 'jsonwebtoken';

@Injectable()
export class JwtMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    const token = req.headers.authorization?.split(' ')[1];
    if (token) {
      try {
        let decoded: any = jwt.verify(token, 'suibianshenme');
        req['user'] = decoded;
      } catch (error) {
        // throw new UnauthorizedException('token验证失败')
      }
    }
    next();
  }
}
